<?php
		
	
	class session extends service {
		
		private $__poi;
		
		public function __construct($service_name = null ){
			parent::__construct($service_name);
			
			if (!session_start()){
				throw new Core_Exception(EX_CRIT, "Failed to start session");
			}

			$cfg = context::get("config");
			if ($cfg["policy"]["session_db_store"]){
				$this->__poi = new db_session($this);
			}
		}
		
		public function notify_error($msg, $level="error"){
			$_SESSION["errors"][] = array("msg"=>$msg,"level"=>$level);
		}
		
		public function get($key){
			if (isset($_SESSION[$key])){
				return $_SESSION[$key];
			}
		}
		
		
		public function validate(){

			$cfg = context::get("config");
			
			$session_security = context::get("session")->get("security");
			
			if ($cfg["policy"]["session_ip_lock"]){
				$ip = $session_security["client"]["ip"];

				
				if ($ip != $_SERVER["REMOTE_ADDR"]){
					context::get("io")->debug("client ip '{$_SERVER["REMOTE_ADDR"]}' not matches with session ip '{$ip}'");
					return false;
				}
			}

			
			if ($this->__poi){
				return $this->__poi->validate();
			}
			return true;
		}
		
		public function install(){
			if ($this->__poi){
				return $this->__poi->install();
			}
		}

	}
	
	class db_session {

		public function __construct(){
			define("NFO2_DB_SESSION", NFO2_DB_PREFIX."session");
		}
		
		public function validate(){

		}
		
		public function install(){
			if(!service::depend_db_table(NFO2_DB_SESSION)){
				$query = "
				";
				context::get("db")->query($query);
			}
		}
	}
	
	
?>
